package cn.hfatec.shms.web;

import cn.hfatec.shms.web.ss.ssCrux.model.SsCrux;
import cn.hfatec.shms.web.ss.ssCrux.service.SsCruxService;
import cn.hfatec.shms.web.ss.ssCustomer.service.SsCustomerService;
import cn.hfatec.shms.web.ss.ssHd.service.SsHdService;
import cn.hfatec.shms.web.ss.ssMonitor.model.SsMonitor;
import cn.hfatec.shms.web.ss.ssMonitor.service.SsMonitorService;
import cn.hfatec.shms.web.xh.xhHfjl.model.XhHfjl;
import cn.hfatec.shms.web.xh.xhHfjl.service.XhHfjlService;
import cn.hfatec.shms.web.yk.ykXmmx.model.YkXmmx;
import cn.hfatec.shms.web.yk.ykXmmx.service.YkXmmxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2016/1/21.
 */
@Component
public class Timer {
    @Autowired
    private YkXmmxService ykXmmxService;
    @Autowired
    private XhHfjlService xhHfjlService;
    @Autowired
    private SsCustomerService ssCustomerService;
    @Autowired
    private SsCruxService ssCruxService;
    @Autowired
    private SsHdService ssHdService;
    @Autowired
    private SsMonitorService ssMonitorService;



/* @Autowired
    private CustomService customService;*/

    /**
     * 凌晨跑批解压未导入的zip包，批量将数据写入到数据库
     */
    @Scheduled(cron = "0 0 22 * * ?")
    public void decompression() throws Exception {

        String hsTime = WeekTime.getBzstart();
        String hsEnd = WeekTime.getBzend(hsTime);
        this.ykXmmxService.deleteYkXmmxhs(hsTime, hsEnd);
        List<YkXmmx> ykjg = this.ykXmmxService.getYkXmmxHs(hsTime, hsEnd);
        for (YkXmmx YkXmmxhs : ykjg) {
            this.ykXmmxService.saveYkXmmx(YkXmmxhs);
        }
    }

    /*
        * 每月1号凌晨1点更新客户的年龄
        *
        * @throws Exception
        */
    @Scheduled(cron = "0 0 23 * * ?")
    public void updateCustomAge() throws Exception {
    /*  List<SsHd> SsHd= this.ssHdService.getSsHdNum();
        if(null == SsHd || SsHd.size() ==0 ){*/
        String testEnd = WeekTime.time();
        String testDate = WeekTime.getDateTime(testEnd, -1);
        List<SsCrux> crux = ssCruxService.selectCrux(testDate, testEnd);
        if (crux != null && !crux.isEmpty()) {
            for (SsCrux ssCrux : crux) {
                SsCrux cruxs = ssCruxService.getSsCruxCruxId(ssCrux.getCruxId());
                if (cruxs != null) {
                    ssCrux.setId(cruxs.getId());
                }
                ssCruxService.saveSsCrux(ssCrux);
            }
        }
        List<SsMonitor> monitor = ssMonitorService.selectMonitor(testDate, testEnd);
        if (monitor != null && !monitor.isEmpty()) {
            for (SsMonitor ssMonitor : monitor) {
                if (ssMonitor.getTypeDif().equals("1")) {
                    ssMonitorService.deleteSsMonitors(ssMonitor.getNian(), ssMonitor.getWeekNum(), ssMonitor.getCustomName(), ssMonitor.getPhone());
                    ssMonitorService.saveSsMonitor(ssMonitor);
                } else {
                    SsMonitor SsMonitors = ssMonitorService.getSsMonitors(ssMonitor.getNian(), ssMonitor.getWeekNum(), ssMonitor.getCustomName(), ssMonitor.getPhone());
                    if (SsMonitors != null) {
                        int res = SsMonitors.getMonitorTime().compareTo(ssMonitor.getMonitorTime());
                        if (res >= 0) {
                            ssMonitorService.deleteSsMonitors(ssMonitor.getNian(), ssMonitor.getWeekNum(), ssMonitor.getCustomName(), ssMonitor.getPhone());
                        }
                        ssMonitorService.saveSsMonitor(ssMonitor);
                    } else {
                        ssMonitorService.saveSsMonitor(ssMonitor);
                    }
                }
            }
        }

            /*}*/
        this.ssCustomerService.updateSsCustomer(testDate, testEnd);


    }


    /**
     * 凌晨跑批解压未导入的zip包，批量将数据写入到数据库
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void decompressionXh() throws Exception {
        Calendar cal = Calendar.getInstance();
        String hsTime = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        this.xhHfjlService.deleteXhHfjls(hsTime, hsTime);
        List<XhHfjl> listxh = xhHfjlService.getXhHfjlList(hsTime, hsTime);
        for (XhHfjl xhHfjl : listxh) {
            xhHfjlService.saveXhHfjl(xhHfjl);
        }
    }
}
